<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Jiankong;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Dept;

class JiankongController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Jiankong(), function (Grid $grid) {
            // $grid->column('id')->sortable();
            $grid->column('ip');
            $grid->column('louhao');
            $grid->column('louceng');
            $grid->column('dept.deptname', '部门名称');
            $grid->column('jkweizhi');
            $grid->column('pinpai');

            $grid->model()->with(['dept']);


            $grid->column('jkpic')->display(function ($imgs) {
                $pics = json_decode($imgs);
                if ($pics) {
                    return $pics[0];
                }
            })->image();;
            $grid->column('beizhu');
            // $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Jiankong(), function (Show $show) {
            $show->field('id');
            $show->field('ip');
            $show->field('jkweizhi');
            $show->field('pinpai');
            $show->field('dept_id');
            $show->jkpic()->image();
            $show->field('beizhu');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Jiankong(), function (Form $form) {
            $form->display('id');
            $form->text('ip');
            $form->text('louhao');
            $form->text('louceng');
            $form->text('jkweizhi');
            $form->text('pinpai');
            $form->text('dept_id');
            $form->multipleImage('jkpic')->accept('jpg,png,gif,jpeg')->limit(6)->autoUpload()->maxSize(20480)->saving(function ($files) {
                return json_encode($files);
            });
            $form->text('beizhu');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
